using System;
using System.Xml;

namespace ECCIT.Configuration
{
	/// <summary>
	/// </summary>
	[Serializable]
	public class AssemblyModelManager : ConfigManager
	{
		private string _assemblyDir;
		private XmlDocument _configDoc;

		public string AssemblyDirectory
		{
			get{ return _assemblyDir; }
		}

		public AssemblyModelManager(){}

		public AssemblyModelManager(string appRootDirectory) : base(appRootDirectory, "AssemblyModel.config")
		{
			_configDoc = GetConfigDocument();
			DirectoryManager dirManager = new DirectoryManager(appRootDirectory);
			_assemblyDir = dirManager.Directories["bin"].ToString();
		}

		public string GetAssemblyName(Type modelType)
		{
			string nameSpace = modelType.Namespace;
			string name = modelType.Name;

			string assemblyName = string.Empty;
			XmlNode modelsNode = _configDoc.DocumentElement.SelectSingleNode("./Assembly/Models[@Namespace='" + nameSpace + "']");
			XmlNode modelNode = modelsNode.SelectSingleNode("./Model[@Name='" + name + "']");

			if( modelNode != null )
				assemblyName = modelNode.ParentNode.ParentNode.Attributes.GetNamedItem("Name").InnerText;
			return assemblyName;
		}
	}
}
